package Interfaces;

import java.awt.Container;
import java.awt.Rectangle;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileNotFoundException;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;

import Modelo.Erros;
import Modelo.Modelo;



public class InsereAlteraFormula extends JFrame{
	private static final long serialVersionUID = 1L;
	private Container cp;
	private JButton cancelar, confirmar, somar, subtrair, multiplicar, dividir, abreParentesis, fechaParentesis, factorial, pi, xquadrado, raiz2, percentagem;
	private JTextArea display;
	private TextField formula, nomebtn;
	private JLabel infoNamebtn;
	private Modelo _modelo;
	private int index;
	private InsereAlteraFormula _this;
	
	private boolean novaFormula;
	
	public InsereAlteraFormula(Modelo modelo, int index, boolean novaFormula) {
		//if boolean true entao Ã© uma nova formula, senao Ã© o alteraFormula
		_this = this;
		
		_modelo = modelo;
		
		this.novaFormula = novaFormula;
		
		this.index = index;
		
		setTitle("Inserir/Alterar nova Fórmula");
		setSize(570, 150);
		setLayout(null);
		_modelo.getInterfacePrincipal().setVisible(false);
		InicilizaComponents();
		SetPosition();
		addComponents();
		addListeners();
		
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		
		setLocationRelativeTo(null);
		
		setVisible(true);
	}
	private void addComponents() {
		cp.add(display);
		cp.add(formula);
		cp.add(nomebtn);
		cp.add(infoNamebtn);
		cp.add(cancelar);
		cp.add(confirmar);
		cp.add(somar);
		cp.add(subtrair);
		cp.add(multiplicar);
		cp.add(dividir);
		cp.add(abreParentesis);
		cp.add(fechaParentesis);
		cp.add(pi);
		cp.add(percentagem);
		cp.add(factorial);
		cp.add(xquadrado);
		cp.add(raiz2);
		
		
	}
	private void SetPosition() {
		display.setBounds(new Rectangle(5, 5, 335, 30));
		cancelar.setBounds(345, 6, 100, 30);
		confirmar.setBounds(450, 6, 100, 30);
		formula.setBounds(new Rectangle(5, 40, 335, 30));
		infoNamebtn.setBounds(340, 40, 100, 30);
		nomebtn.setBounds(450, 40, 100, 30);
		
		somar.setBounds(5, 75, 45, 30);
		subtrair.setBounds(55, 75, 45, 30);
		multiplicar.setBounds(105,  75, 45, 30);
		dividir.setBounds(155, 75, 45, 30);
		abreParentesis.setBounds(205, 75, 45, 30);
		fechaParentesis.setBounds(255, 75, 45, 30);
		factorial.setBounds(305, 75, 45, 30);
		pi.setBounds(355, 75, 45, 30);
		xquadrado.setBounds(405, 75, 45, 30);
		raiz2.setBounds(455, 75, 45, 30);
		percentagem.setBounds(505, 75, 45, 30);
	}
	private void InicilizaComponents() {
		cp=getContentPane();
		
		display=new JTextArea(_modelo.getFormula(index));
		
		display.setEnabled(false);
		
		formula = new TextField("");
		
		if(novaFormula)
			formula = new TextField("");
		else
			formula = new TextField(_modelo.getFormula(index));
		
		nomebtn=new TextField(_modelo.getEqNome(index));
		
		infoNamebtn=new JLabel("Nome da fórmula: ");
		
		cancelar=new JButton("Cancelar");
		
		confirmar=new JButton("Confirmar");
		
		
		somar=new JButton("+");
		subtrair=new JButton("-");
		multiplicar=new JButton("*");
		dividir=new JButton("/");
		abreParentesis=new JButton("(");
		fechaParentesis=new JButton(")");
		factorial=new JButton("!");
		pi=new JButton("π");
		xquadrado=new JButton("x^²");
		raiz2=new JButton("√");
		percentagem=new JButton("%");
	}
	
	private void addListeners(){
		cancelar.addActionListener(new cancelarListener());
		confirmar.addActionListener(new confirmarListener());
		somar.addActionListener(new somarListener());
		subtrair.addActionListener(new subtrairListener());
		multiplicar.addActionListener(new multiplicarListener());
		dividir.addActionListener(new dividirListener());
		abreParentesis.addActionListener(new abreParentesisListener());
		fechaParentesis.addActionListener(new fechaParentesisListener());
		factorial.addActionListener(new factorialListener());
		pi.addActionListener(new piListener());
		xquadrado.addActionListener(new xquadradoListener());
		raiz2.addActionListener(new raiz2Listener());
		percentagem.addActionListener(new percentagemListener());
	}

	public class cancelarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) 
		{
			sair();
		}
    }
	
	
	private void sair()
	{
		_modelo.getInterfacePrincipal().setVisible(true);
		
		_this.dispose();
	}
	
	public class confirmarListener implements ActionListener
	{
		@Override
		public void actionPerformed(ActionEvent e)
		{
			for(int i = 0; i < 18; i++)
				if(_modelo.getEqNome(i).equalsIgnoreCase(nomebtn.getText()) && i != index)
				{
					JOptionPane.showMessageDialog(_this, "Esse nome já foi escolhido.", "Estado", JOptionPane.QUESTION_MESSAGE);
					return;
				}
			
			if(formula.getText().length() == 0)
			{
				if(_modelo.isVazio(index))
				{
					sair();
					return;
				}
				
				_modelo.limpaFormula(index);
				
				JOptionPane.showMessageDialog(_this, "Eleminou a formula associada a este botão.", "Estado", JOptionPane.QUESTION_MESSAGE);
				
				_modelo.getInterfacePrincipal().setFName(index, _modelo.getEqNome(index));
				
				try 
				{
					_modelo.saveFormulas();
				} 
				catch (IOException e1) 
				{
					e1.printStackTrace();
				}
				
				sair();
				return;
			}
			
			Erros id;
			
			try 
			{
				id = valida(formula.getText());
				
				if(id == Erros.VALIDO)
				{
					_modelo.setEqNome(nomebtn.getText(), index);
					
					_modelo.saveFormulas();
					
					_modelo.getInterfacePrincipal().setFName(index, _modelo.getEqNome(index));
					
					_modelo.getInterfacePrograma().getVisor().setText("\n\t" + _modelo.getEqNome(index) + ": \n\t\t" + _modelo.getFormula(index));
					
					_modelo.getInterfacePrograma().repaint();
					
					_modelo.getInterfacePrograma().getVisor().repaint();
					
					sair();
				}
			} 
			catch (FileNotFoundException e1) 
			{
				e1.printStackTrace();
			} 
			catch (IOException e1)
			{
				e1.printStackTrace();
			}
		}
    }
	
	public class somarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'+');
		}
    }
	public class subtrairListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'-');
		}
    }
	public class multiplicarListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'*');
		}
    }
	public class dividirListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'/');
		}
    }
	public class abreParentesisListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'(');
		}
    }
	public class fechaParentesisListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+')');
		}
    }
	public class factorialListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'!');
		}
    }
	public class piListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'π');
		}
    }
	public class xquadradoListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+"^2");
		}
    }
	public class raiz2Listener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+"√");
		}
    }
	public class percentagemListener implements ActionListener {
		@Override
		public void actionPerformed(ActionEvent e) {
			formula.setText(formula.getText()+'%');
		}
    }
	
	private Erros valida(String formula) throws FileNotFoundException, IOException
	{
		Erros id = _modelo.adicionaEq(formula, index);
		
		JOptionPane.showMessageDialog(_this, Erros.mensagem(id), "Estado", JOptionPane.QUESTION_MESSAGE);
		
		return id;
	}
}
